package com.easyedu.dean.service;

import com.easyedu.core.redis.RedisUtils;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * @author lmr
 **/
@Component
public class ScheduleService implements SchedulingConfigurer {

    private final RedisUtils redisUtils;

    private final OpenCourseService openCourseService;

    private static final String SELECTION_SYSTEM_IS_OPEN = "is_open_selection_system";
    /**
     * 功能描述: 每天4：30重复
     **/
    private static final String CRON = "0 30 4 * * ?";

    public ScheduleService(OpenCourseService openCourseService, RedisUtils redisUtils) {
        this.openCourseService = openCourseService;
        this.redisUtils = redisUtils;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        //定时任务内容
        scheduledTaskRegistrar.addTriggerTask(() -> {
                    if (redisUtils.get(SELECTION_SYSTEM_IS_OPEN) != null && redisUtils.get(SELECTION_SYSTEM_IS_OPEN).equals("true")) {
                        openCourseService.HasOpenCourseToRedis();
                    }
                }, triggerContext -> {
                    CronTrigger cronTrigger = new CronTrigger(CRON);
                    return cronTrigger.nextExecutionTime(triggerContext);
                }
        );
    }
}
